package com.zy.hospitalward_management_system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.hospitalward_management_system.pojo.Staff;
import com.zy.hospitalward_management_system.service.impl.StaffServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zy
 * @since 2021-01-26
 */
@Controller
public class StaffController {

    @Resource
    StaffServiceImpl staffService;

    @PostMapping("/staff/update")
    @ResponseBody
    public String updateStaff(Staff staff, HttpSession session){

        if(staff.getStaffUsername().isEmpty()){
            return "请输入用户名!";
        }

        if(staff.getStaffName().isEmpty()){
            return "请输入员工姓名!";
        }

        if(staff.getStaffPhone().isEmpty()){
            return "请输入员工联系方式!";
        }

        if(staff.getStaffAge()==null){
            return "请输入员工年龄!";
        }

        System.out.println(staff);
        staff.setUpdateTime(new Date());
        int i = staffService.getBaseMapper().updateById(staff);
        if(i==1){
            return "修改成功!";
        }else {
            return "修改失败!";
        }
    }


    /**
     * 跳转到添加员工页面
     */
    @RequestMapping("/toAddStaff")
    public String toAddStaff(){
        return "user/add/staff_add";
    }


    /**
     * 跳转到员工管理页面
     */
    @RequestMapping("/toStaffList")
    public String toStaffList(Model model){
        List<Staff> staffs = staffService.getBaseMapper().selectList(new QueryWrapper<Staff>().ne("staff_grade", 1));
        model.addAttribute("staffs",staffs);
        return "user/list/staff_list";
    }

    /**
     * 传入员工对象,添加一名新员工
     * @param staff 员工对象
     * @return 返回信息
     */
    @PostMapping("/staff/add")
    @ResponseBody
    public String addStaff(Staff staff){

        if(staff.getStaffUsername().isEmpty()){
            return "请输入用户名!";
        }

        if(staff.getStaffName().isEmpty()){
            return "请输入员工姓名!";
        }

        if(staff.getStaffPhone().isEmpty()){
            return "请输入员工联系方式!";
        }

        if(staff.getStaffAge()==null){
            return "请输入员工年龄!";
        }

        staff.setStaffPassword("123456");
        staff.setCreateTime(new Date());
        staff.setUpdateTime(new Date());
        staff.setStaffGrade(0);

        Staff staff1 = staffService.getBaseMapper().selectOne(new QueryWrapper<Staff>().eq("staff_username", staff.getStaffUsername()));
        if(staff1!=null){
            return "该用户名已经被占用!";
        }

        int insert = staffService.getBaseMapper().insert(staff);
        if(insert==1){
            return "添加成功!默认密码为123456,请及时联系员工登录更改.";
        }else {
            return "添加失败!";
        }

    }

    @RequestMapping("/staff/delete/{id}")
    public String deleteStaff(@PathVariable("id") String id){

        staffService.getBaseMapper().deleteById(Integer.parseInt(id));

        return "redirect:/toStaffList";
    }


}

